<?php

namespace App\Admin\Controllers;

use App\Models\Book;
use App\Models\BookCategory;
use App\Models\BookSelf;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class BookController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Book(), function (Grid $grid) {
            $grid->column('title')->display(function ($d){
                return '<img src="/'. $this->cover.'" style="width:50px;margin-right:10px" />' . $d;
            });
            $grid->column('selves_id')->display(function ($d){
                $m = BookSelf::with('floor')->find($d);
                return $m->floor->name . ' - ' . $m->name;
            });
            $grid->column('category_id')->display(function ($d){
                return BookCategory::find($d)->name;
            });
            $grid->column('author');
            $grid->column('publisher');

            $grid->column('stock');
            $grid->column('borrow_num');
            $grid->column('view_num');
            $grid->column('sort');
//            $grid->column('status');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('name');
                $filter->equal('author');
                $filter->equal('publisher');
                $filter->equal('category_id')->select(BookCategory::all()->pluck('name', 'id'));

            });

            $grid->showQuickEditButton();
            $grid->disableViewButton();
            $grid->disableEditButton();
        });
    }


    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Book(), function (Form $form) {
            $form->display('id');
            $form->text('title')->required();
            $form->select('selves_id')->options(BookSelf::getArea())->required();;
            $form->select('category_id')->options(BookCategory::all()->pluck('name', 'id'))->required();;
            $form->text('author')->required();;
            $form->text('publisher')->required();;
            $form->image('cover')->autoUpload()->required()->uniqueName();
            $form->number('stock')->default(3)->required();;
            $form->number('sort')->default(1)->required();;
            $form->radio('status')->options(Book::STATUS_ENUM)->default(Book::STATUS_NORMAL);
            $form->editor('description')->required();;

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
